How to convert string to number in TypeScript ?

您所在的位置:网站首页 typescript int How to convert string to number in TypeScript ?

How to convert string to number in TypeScript ?

2024-07-13 15:32| 来源: 网络整理| 查看: 265

In typescript, there are numerous ways to convert a string to a number. We can use the ‘+’ unary operator , Number(), parseInt() or parseFloat() function to convert string to number. Let’s demonstrate using a few examples.

Table of Content

Using the ‘+’ unary operatorUsing Number() methodUsing parseFloat() functionUsing Number.parseInt()Using String.prototype.charCodeAt() and Array.prototype.reduce()Using Regular Expressions1. Using the ‘+’ unary operator

The unary plus operator (`+`) in TypeScript converts a string to a number by parsing its content. It coerces the string representation of numeric characters into a numerical value, ensuring type conversion.

Example 1:  The following code demonstrates converting a string to a number by using the ‘+’ unary operator.

JavaScript let str: string = "431"; console.log(typeof str); let num = +str; console.log(typeof num);

Output:

stringnumber2. Using Number() method

The Number() method in TypeScript converts a string to a number by explicitly invoking the Number constructor. It parses the string’s content to a numerical value, ensuring type conversion.

Example 2: The following code demonstrates converting a string to a number by using the Number() method. Instead of using the ‘+’ operator, we can use the Number() function to convert string to number. The string must be given as an argument to the Number() function.

JavaScript let str: string = "431"; console.log(typeof str); let num = Number(str); console.log(typeof num);

Output:

stringnumber3. Using parseFloat() function

The parseFloat() function in TypeScript converts a string to a floating-point number by parsing its content. It extracts and interprets the numerical portion of the string, ensuring type conversion.

Example : Numbers can be of type float or int. To convert a string in the form of float to a number we use the parseFloat() function and to convert strings that do not have decimal to a number, the parseInt() function is used. 

JavaScript let str1:string = "102.2"; console.log(typeof str1); let num = parseFloat(str1); console.log(`${num}` + " is of type :" + typeof num); let str2:string = "61"; console.log(typeof str2); let num2 = parseInt(str2); console.log(`${num2}` + " is of type :" + typeof num2);

Output:

string102.2 is of type :numberstring61 is of type :number4. Using Number.parseInt()

The Number.parseInt() method parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems). It’s particularly useful when you want to convert a string to an integer, optionally with a specified radix.

Example:

JavaScript let str: string = "431"; console.log(typeof str); let num = Number.parseInt(str); console.log(typeof num);

Output:

stringnumber5. Using String.prototype.charCodeAt() and Array.prototype.reduce()

We can leverage the charCodeAt() method along with the reduce() method of arrays to convert a string representing a numeric value to a number in TypeScript. This approach involves converting each character of the string to its Unicode code point and then reconstructing the numeric value based on these code points.

Example: In this example we are following above explained apporach.

JavaScript let str: string = "431"; console.log(typeof str); // Convert string to number using charCodeAt() and reduce() let num = str.split('').reduce((acc, char) => acc * 10 + (char.charCodeAt(0) - 48), 0); console.log(typeof num);

Output:

stringnumber6. Using Regular Expressions

Regular expressions provide a powerful tool for pattern matching and manipulation in TypeScript. By leveraging regular expressions, we can extract numerical values from strings and convert them to numbers.

Example:

JavaScript let str: string = "The price is $25.99"; console.log(typeof str); // Output: string // Extracting numerical values using regular expression let num: number = parseFloat(str.match(/\d+\.\d+/)[0]); console.log(`${num} is of type: ${typeof num}`); // Output: 25.99 is of type: number

Output:

25.99 is of type: number

S

sarahjane3102 Improve Next Article How to Convert Number to String in TypeScript ? Please Login to comment...


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3